package com.darren.befriend.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Switch;
import android.widget.TextView;

import com.darren.befriend.HomeActivity;
import com.darren.befriend.R;
import com.darren.befriend.utilities.LogUtil;
import com.darren.befriend.views.ChildrenSegment;
import com.darren.befriend.views.ChildrenSegment.OnChildrenChangeListener;
import com.darren.befriend.views.RangeSeekBar;
import com.darren.befriend.views.RangeSeekBar.OnRangeSeekBarChangeListener;
import com.darren.befriend.views.UserSettingItemView;
import com.darren.befriend.views.UserSettingItemView.OnCellDirectListener;
import com.parse.ParseObject;
import com.parse.ParseUser;

public class SearchFilterFragment extends Fragment implements
		OnSeekBarChangeListener, OnCheckedChangeListener {

	private ViewGroup mainView;
	private UserSettingItemView mFriendFor;
	private Switch mMen;
	private Switch mWomen;
	private UserSettingItemView mPersonality;
	private ChildrenSegment mChildrendSeg;
	private UserSettingItemView mInterest;
	private UserSettingItemView mSport;
	private UserSettingItemView mFilm;
	private UserSettingItemView mMusic;
	private SeekBar mWithinSeekbar;
	private TextView mWithinValue;
	private ViewGroup mAgeSeekbarView;
	private TextView mAgeValue;
	private TextView mMoneyValue;
	private ViewGroup mMoneySeekbarView;
	private RangeSeekBar<Integer> mAgeSeekBar;
	private RangeSeekBar<Integer> mMoneySeekBar;

	private ParseObject mFilter;
	private static ParseUser mCurrentUser;

	public static Fragment create(ParseUser currentUser) {
		SearchFilterFragment fragment = new SearchFilterFragment();
		mCurrentUser = currentUser;
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mainView = (ViewGroup) inflater.inflate(
				R.layout.layout_search_filter_fragment, container, false);
		initViews();

		((HomeActivity) getActivity()).setContent(this);
		if (mCurrentUser != null) {
			mFilter = (ParseObject) mCurrentUser.get("Filter");
			if (mFilter != null) {

				initData();
			} else {

				try {

					mFilter = new ParseObject("Filter");
					mCurrentUser.put("Filter", mFilter);
				} catch (Exception ex) {

				}
			}
		}

		return mainView;
	}

	@Override
	public void onPause() {
		super.onPause();
		mCurrentUser.saveInBackground();
	}

	/**
	 * initViews
	 */
	private void initViews() {
		mFriendFor = (UserSettingItemView) mainView
				.findViewById(R.id.friend_for_container);
		mFriendFor.setLeftTitle(getString(R.string.friend_for));
		mFriendFor.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& getActivity() instanceof HomeActivity) {
					Fragment fragment = FriendForFragment.create();
					((HomeActivity) getActivity())
							.switchContentWithNormalAnimation(fragment);
				}
			}
		});
		mMen = (Switch) mainView.findViewById(R.id.men_swicth);
		mMen.setOnCheckedChangeListener(this);
		mWomen = (Switch) mainView.findViewById(R.id.women_swicth);
		mWomen.setOnCheckedChangeListener(this);
		mWithinValue = (TextView) mainView.findViewById(R.id.within_value);
		mWithinSeekbar = (SeekBar) mainView.findViewById(R.id.within_seekbar);
		mWithinSeekbar.setOnSeekBarChangeListener(this);
		mAgeValue = (TextView) mainView.findViewById(R.id.age_value);
		mAgeSeekbarView = (ViewGroup) mainView
				.findViewById(R.id.age_seekbar_container);
		if (mAgeSeekbarView != null) {
			mAgeSeekBar = new RangeSeekBar<Integer>(18, 40, getActivity());
			mAgeSeekBar
					.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
						@Override
						public void onRangeSeekBarValuesChanged(
								RangeSeekBar<?> bar, Integer minValue,
								Integer maxValue) {
							mAgeValue.setText(String.format(
									getString(R.string.age_range), minValue,
									maxValue));
							reportAge(minValue, maxValue);
						}
					});
			mAgeSeekbarView.addView(mAgeSeekBar);
		}

		mMoneyValue = (TextView) mainView.findViewById(R.id.money_value);
		mMoneySeekbarView = (ViewGroup) mainView
				.findViewById(R.id.money_seekbar_container);
		if (mMoneySeekbarView != null) {
			mMoneySeekBar = new RangeSeekBar<Integer>(20, 1000, getActivity());
			mMoneySeekBar
					.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
						@Override
						public void onRangeSeekBarValuesChanged(
								RangeSeekBar<?> bar, Integer minValue,
								Integer maxValue) {
							mMoneyValue.setText(String.format(
									getString(R.string.money_range), minValue,
									maxValue));
							reportMoney(minValue, maxValue);
						}
					});
			mMoneySeekbarView.addView(mMoneySeekBar);
		}
		mPersonality = (UserSettingItemView) mainView
				.findViewById(R.id.personality_container);
		mPersonality.setLeftTitle(getString(R.string.personality));
		mPersonality.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& getActivity() instanceof HomeActivity) {
					Fragment fragment = PersonalityFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					((HomeActivity) getActivity())
							.switchContentWithNormalAnimation(fragment);
				}
			}
		});

		//
		mInterest = (UserSettingItemView) mainView
				.findViewById(R.id.interest_container);
		mInterest.setLeftTitle(getString(R.string.interest));
		mInterest.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& getActivity() instanceof HomeActivity) {
					Fragment fragment = InterestFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					((HomeActivity) getActivity())
							.switchContentWithNormalAnimation(fragment);
				}
			}
		});
		mSport = (UserSettingItemView) mainView
				.findViewById(R.id.sport_container);
		mSport.setLeftTitle(getString(R.string.sports));
		mSport.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& getActivity() instanceof HomeActivity) {
					Fragment fragment = SportsFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					((HomeActivity) getActivity())
							.switchContentWithNormalAnimation(fragment);
				}
			}
		});
		mFilm = (UserSettingItemView) mainView
				.findViewById(R.id.film_container);
		mFilm.setLeftTitle(getString(R.string.film));
		mFilm.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& getActivity() instanceof HomeActivity) {
					Fragment fragment = FilmFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					((HomeActivity) getActivity())
							.switchContentWithNormalAnimation(fragment);
				}
			}
		});
		mMusic = (UserSettingItemView) mainView
				.findViewById(R.id.music_container);
		mMusic.setLeftTitle(getString(R.string.music));
		mMusic.setOnCellDirectListener(new OnCellDirectListener() {

			@Override
			public void onCellClick() {
				if (getActivity() != null
						&& getActivity() instanceof HomeActivity) {
					Fragment fragment = MusicFragment
							.create(HobbyBaseFragment.SECOND_MODE);
					((HomeActivity) getActivity())
							.switchContentWithNormalAnimation(fragment);
				}
			}
		});
		mChildrendSeg = (ChildrenSegment) mainView
				.findViewById(R.id.children_segment);
		mChildrendSeg.setSelected(0);
		mChildrendSeg.setOnGenderListener(new OnChildrenChangeListener() {

			@Override
			public void onChange(int isSelected) {
				reportChildren(isSelected);
			}
		});
	}

	private void initData() {
		if (mFilter.isDataAvailable()) {
			mMen.setChecked((Boolean) mFilter.get("Men"));
			mWomen.setChecked((Boolean) mFilter.get("Women"));
			int within = (Integer) mFilter.get("Within");
			mWithinValue.setText(String.format(
					getString(R.string.within_distance), within));
			mWithinSeekbar.setProgress(within);
			int startAge = (Integer) mFilter.get("StartAge");
			int endAge = (Integer) mFilter.get("EndAge");
			mAgeSeekBar.setSelectedMinValue(startAge);
			mAgeSeekBar.setSelectedMaxValue(endAge);
			if (endAge > 40) {
				mAgeValue.setText(String.format(
						getString(R.string.age_range_plus), startAge,
						endAge - 1));
			} else {
				mAgeValue.setText(String.format(getString(R.string.age_range),
						startAge, endAge));
			}

			int startMoney = (Integer) mFilter.get("StartMoney");
			int endMoney = (Integer) mFilter.get("EndMoney");
			mMoneySeekBar.setSelectedMinValue(startMoney);
			mMoneySeekBar.setSelectedMaxValue(endMoney);
			if (endMoney > 1000) {
				mMoneyValue.setText(String.format(
						getString(R.string.money_range_plus), startMoney));
			} else {
				mMoneyValue.setText(String.format(
						getString(R.string.money_range), startMoney, endMoney));
			}

			int index = 0;
			try {

				index = (Integer) mFilter.get("Child");
			} catch (NullPointerException npe) {

				LogUtil.e("initData", "Null pointer");
			}

			mChildrendSeg.setSelected(index);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (getActivity() instanceof HomeActivity) {
			((HomeActivity) getActivity())
					.setActivityTitle(getString(R.string.search_filter));
			((HomeActivity) getActivity()).setLeftImage(R.drawable.item_back);
			((HomeActivity) getActivity()).showRightButtonImg(false);
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		mWithinValue.setText(String.format(getString(R.string.within_distance),
				progress));
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		reportWithin(seekBar.getProgress());
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		int id = buttonView.getId();
		switch (id) {
		case R.id.men_swicth:
			reportMen(isChecked);
			break;
		case R.id.women_swicth:
			reportWoMen(isChecked);
			break;

		default:
			break;
		}
	}

	/**
	 * update age filter
	 *
	 * @param startAge
	 * @param endAge
	 */
	private void reportAge(int startAge, int endAge) {
		if (mFilter != null) {

			mFilter.put("StartAge", startAge);
			mFilter.put("EndAge", endAge);
		}
	}

	/**
	 * update money filter
	 *
	 * @param startAge
	 * @param endAge
	 */
	private void reportMoney(int startAge, int endAge) {

		if (mFilter != null) {

			mFilter.put("StartMoney", startAge);
			mFilter.put("EndMoney", endAge);
		}
	}

	/**
	 * report men
	 *
	 * @param checked
	 */
	private void reportMen(boolean checked) {

		if (mFilter != null) {

			mFilter.put("Men", checked);
		}
	}

	/**
	 * report women
	 *
	 * @param checked
	 */
	private void reportWoMen(boolean checked) {

		if (mFilter != null) {

			mFilter.put("Women", checked);
		}
	}

	/**
	 * report within
	 *
	 * @param within
	 */
	private void reportWithin(int within) {

		if (mFilter != null) {

			mFilter.put("Within", within);
		}
	}

	private void reportChildren(int index) {

		if (mFilter != null) {

			mFilter.put("Child", index);
		}
	}
	
}
